package com.cgg.stock.common.exception;

import com.cgg.stock.common.util.ResponseData;
import com.cgg.stock.common.util.ResponseDataUtil;
import com.cgg.stock.common.util.ResultEnums;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseData handleException(Exception e){
        //如果是tokenException，响应401，未认证
        if (e instanceof TokenException){
            return new ResponseData(ResultEnums.UNAUTHORIZED.getStatus(),e.getMessage());
        }
        //业务异常
        if(e instanceof BaseException){
            return ResponseDataUtil.buildError(e.getMessage());
        }

        //系统异常
        return ResponseDataUtil.buildError("未知错误,请联系管理员");
    }
}
